#include<mutex>
#include<iostream>
using namespace std;


//饿汉模式
//class Singleton
//{
//public:
//  static Singleton* GetInstance()
//  {
//    cout << &_inst << endl;
//    return &_inst;
//  }
//private:
//  static Singleton _inst;
//private:
//  Singleton()
//  {}
//  Singleton(const Singleton& x) = delete;
//  Singleton& operator=(const Singleton& x) = delete; 
//};
// //静态成员在类外初始化
// Singleton Singleton::_inst;
//


//懒汉模式
class Singleton
{
public:
  static void Destroy()
  {
    cout <<"destroy Singleton..." << endl;
    if(_pinst)
    {
      delete  _pinst;
      _pinst = nullptr;
    }
  }
  static Singleton* GetSingLeton()
  {
    if(_pinst == nullptr)
    {
      unique_lock<mutex> uq(_mtx);
      if(_pinst == nullptr)
      {
        atexit(Destroy);
        _pinst = new Singleton();
      }
    }
    return _pinst;
  }
  ////实现一个内部类,完成对单例对象的销毁
  //class CGarbo
  //{
  //public:
  // ~CGarbo()
  // {
  //   if(_pinst)
  //   {
  //     cout << "~CGarbo" << endl;
  //     delete _pinst;
  //     _pinst = nullptr;
  //   }

  // }
  //};
private:
  Singleton()
  {}
  Singleton(const Singleton& x) = delete;
  Singleton& operator=(const Singleton& x) = delete; 
  
  //static CGarbo cb; 
  static Singleton* _pinst;
  static mutex _mtx;
};
//Singleton::CGarbo  cb;
Singleton* Singleton::_pinst = nullptr;
mutex Singleton::_mtx;
